/** 
 * \file gdrot.cu
 * \author Kyle E. Niemeyer
 * \date 10/03/2011
 *
 * Based on "drot.f" from BLAS.
 *
 */

////////////////////////////////////////////////////////////////////////

/** gdrot applies a plane rotation 
 *
 * \param[in]     n     vector dimension
 * \param[in,out] dx    x array, dimension n
 * \param[in]     incx  increment between elements of dx
 * \param[in,out] dy    y array, dimension n
 * \param[in]     incy  increment between elements of dy
 * \param[in]     c     cosine of angle of rotation
 * \param[in]     s     sine of angle of rotation
 */
__device__ __inline__ void gdrot ( int n, double *dx, int incx, double *dy, double dc, double ds )
{
    
  if ( n <= 0 ) return;
  
  if ( ( incx == 1 ) && ( incy == 1 ) ) {
    
    // both increments equal to 1
    
    for ( uint i = 0; i < n; ++i ) {
      double dtemp = dc * dx[i] + ds * dy[i];
      dy[i] = dc * dy[i] - ds * dx[i];
      dx[i] = dtemp;
    }
    
  } else {
    
    // unequal increments or equal increments not equal to 1
    
    int ix = 1;
    int iy = 1;
    
    if ( incx < 0 ) ix = (-n + 1) * incx + 1;
    if ( incy < 0 ) iy = (-n + 1) * incy + 1;
    
    for ( uint i = 0; i < n; ++i ) {
      double dtemp = dc * dx[ix] + ds * dy[iy];
      dy[iy] = dc * dy[iy] - ds * dx[ix];
      dx[ix] = dtemp;
      ix += + incx;
      iy += + incy;
    }
    
  }
  
}